package cn.jsj.dao;

import java.util.List;

import org.hibernate.criterion.Restrictions;

import cn.jsj.pojo.TblLawyer;
import cn.jsj.pojo.TblPracticeLawyer;

/**
 * 实习律师处理类
 * @author Administrator
 *
 */

public class TblPracticeLawyerDAO {

	/**
	 * 验证登陆用户是否存在
	 * @param ui：登陆用户对象
	 * @return：用户对象：null：不存在
	 */
	public TblPracticeLawyer userValidate(TblPracticeLawyer ui){
		// 取得session对象
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		List userList = session.createCriteria(TblPracticeLawyer.class).add(
				Restrictions.eq("pracLawName", ui.getPracLawName()))
				.add(Restrictions.eq("lawyerPwd", ui.getLawyerPwd()))
				.list();
		// 提交事务
		tx.commit();
		session.close();
		//如果用户存在
		if(userList.size()!=0)
		{
			TblPracticeLawyer user=(TblPracticeLawyer)userList.get(0);
		return user;
		}
		else{
			return null;
		}
		}

	/**
	 * 获取实习律师信息条数
	 * @return:实习律师信息条数
	 */
	public int getPracLawyerInfoNumb(){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		List result=session.createCriteria(TblPracticeLawyer.class)
		.add(Restrictions.eq("deleteLabel", 0))
		.list();
		// 提交事务
		tx.commit();
		session.close();
		return result.size();
	}


	/**
	 * 获取指定页面要显示的实习律师信息
	 * @param startNumb：从第几条开始
	 * @param totalNumb：这一页显示几条
	 * @return：这一页要显示的实习律师信息队列
	 */
	public java.util.List<TblPracticeLawyer> getThePracLawyerInfo(cn.jsj.bgtodb.PageRestrictProperty prp){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		List result=session.createCriteria(TblPracticeLawyer.class)
		.add(Restrictions.eq("deleteLabel", 0))
		.setFirstResult(prp.getStartNumb())
		.setMaxResults(prp.getTotalNumb())
		.list();
		// 提交事务
		tx.commit();
		session.close();
		return result;
	}


	/**
	 * 获得要查询的实习律师信息条数
	 * @param pracLawyerName：实习律师的名字
	 * @param guideTeacher：指导老师
	 * @return：符合条件的实习律师信息条数
	 */
	public int getSearchPracLawyerInfoNumb(cn.jsj.bgtodb.SearchPracLawyerProperty splp){
		/*
		 * 有三种情况
		 * 1.pracLawyerName为NULL guideTeacher不为NULL
		 * 2.pracLawyerName不为NULL guideTeacher为NULL
		 * 3.pracLawyerName不为NULL guideTeacher不为NULL
		 */

		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();

		int teacherId=0;
		if(splp.getPracGuideTeacher().length()!=0){
		//根据指导老师姓名找到对应的ID
		cn.jsj.dao.TblLawyerDAO tld=new TblLawyerDAO();
		teacherId=tld.getLawyerId(splp.getPracGuideTeacher());
		}
		//查询结果队列
		List result=new java.util.LinkedList();

		//第一种情况
		if(splp.getPracLawyerName().length()==0&&splp.getPracGuideTeacher().length()!=0){
			result=session.createCriteria(TblPracticeLawyer.class)
			.add(Restrictions.eq("guidTeacher", teacherId))
			.add(Restrictions.eq("deleteLabel", 0))
			.list();
		}

		//第二种情况
		if(splp.getPracLawyerName().length()!=0&&splp.getPracGuideTeacher().length()==0){
			result=session.createCriteria(TblPracticeLawyer.class)
			.add(Restrictions.eq("pracLawName", splp.getPracLawyerName()))
			.add(Restrictions.eq("deleteLabel", 0))
			.list();
		}

		//第三种情况
		if(splp.getPracLawyerName().length()!=0&&splp.getPracGuideTeacher().length()!=0){
			result=session.createCriteria(TblPracticeLawyer.class)
			.add(Restrictions.eq("pracLawName", splp.getPracLawyerName()))
			.add(Restrictions.eq("guidTeacher", teacherId))
			.add(Restrictions.eq("deleteLabel", 0))
			.list();
		}

		// 提交事务
		tx.commit();
		session.close();
		return result.size();
	}



	/**
	 * 获得要查询的实习律师信息
	 * @param pracLawyerName：实习律师的名字
	 * @param guideTeacher：指导老师
	 * @param startNumb：从第几条开始
	 * @param totalNumb：这一页显示几条
	 * @return：符合条件的实习律师信息
	 */
	public List<TblPracticeLawyer> getSearchPracLawyerInfo(cn.jsj.bgtodb.SearchPracLawyerProperty splp){
		/*
		 * 有三种情况
		 * 1.pracLawyerName为NULL guideTeacher不为NULL
		 * 2.pracLawyerName不为NULL guideTeacher为NULL
		 * 3.pracLawyerName不为NULL guideTeacher不为NULL
		 */

		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();

		int teacherId=0;
		if(splp.getPracGuideTeacher().length()!=0){
		//根据指导老师姓名找到对应的ID
		cn.jsj.dao.TblLawyerDAO tld=new TblLawyerDAO();
		teacherId=tld.getLawyerId(splp.getPracGuideTeacher());
		}
		//查询结果队列
		List result=new java.util.LinkedList();

		//第一种情况
		if(splp.getPracLawyerName().length()==0&&splp.getPracGuideTeacher().length()!=0){
			result=session.createCriteria(TblPracticeLawyer.class)
			.add(Restrictions.eq("guidTeacher", teacherId))
			.add(Restrictions.eq("deleteLabel", 0))
			.setFirstResult(splp.getStartNumb())
		    .setMaxResults(splp.getTotalNumb())
			.list();
		}

		//第二种情况
		if(splp.getPracLawyerName().length()!=0&&splp.getPracGuideTeacher().length()==0){
			result=session.createCriteria(TblPracticeLawyer.class)
			.add(Restrictions.eq("pracLawName", splp.getPracLawyerName()))
			.add(Restrictions.eq("deleteLabel", 0))
			.setFirstResult(splp.getStartNumb())
		    .setMaxResults(splp.getTotalNumb())
			.list();
		}

		//第三种情况
		if(splp.getPracLawyerName().length()!=0&&splp.getPracGuideTeacher().length()!=0){
			result=session.createCriteria(TblPracticeLawyer.class)
			.add(Restrictions.eq("pracLawName", splp.getPracLawyerName()))
			.add(Restrictions.eq("guidTeacher", teacherId))
			.add(Restrictions.eq("deleteLabel", 0))
			.setFirstResult(splp.getStartNumb())
		    .setMaxResults(splp.getTotalNumb())
			.list();
		}

		// 提交事务
		tx.commit();
		session.close();
		return result;
	}


	/**
	 * 修改实习律师信息
	 * @param tl：修改之后的律师信息对象
	 */
	public void modifyPracLawyerInfo(TblPracticeLawyer tl){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();

		//获得时间字符串表示
		java.text.DateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd");
		String dateString=df.format(tl.getBirthTime());
		String endTime=df.format(tl.getEndTime());
		String startTime=df.format(tl.getStartTime());

		cn.jsj.utils.SysLogger.PrintInfo(dateString+endTime+startTime);

		String hql="update TblPracticeLawyer m set m.pracLawName=?,m.pracLawSex=?,m.politicalStatus=?," +
				"m.educationalLevel=?,m.graduateSchool=?,m.pracLawPhone=?,m.endTime=?,m.startTime=?,m.pracAgreement=?," +
				"m.identityNumber=?,m.parcLawEmail=?,m.birthTime=?,m.practiceNumber=?," +
				"m.guidTeacher=?where m.pracLawId=?";
		session.createQuery(hql).setString(0,tl.getPracLawName())
		.setString(1,tl.getPracLawSex())
		.setString(2,tl.getPoliticalStatus())
		.setString(3, tl.getEducationalLevel())
		.setString(4, tl.getGraduateSchool())
		.setString(5, tl.getPracLawPhone())
		.setString(6,endTime)
		.setString(7, startTime)
		.setInteger(8, tl.getPracAgreement())
		.setString(9, tl.getIdentityNumber())
		.setString(10, tl.getParcLawEmail())
		.setString(11, dateString)
		.setString(12,tl.getPracticeNumber())
		.setInteger(13, tl.getGuidTeacher())
		.setInteger(14, tl.getPracLawId())
		.executeUpdate();
		// 提交事务
		tx.commit();
		session.close();
	}


	/**
	 * 删除实习律师信息
	 * @param pracLawyerId：要被删除的实习律师信息ID
	 * @return:1：删除成功
	 */
	public int deletePracLawyerInfo(int pracLawyerId){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		String hql="update TblPracticeLawyer m set m.deleteLabel=?where m.pracLawId=?";
		session.createQuery(hql)
		.setInteger(0, 1)
		.setInteger(1, pracLawyerId)
		.executeUpdate();
		// 提交事务
		tx.commit();
		session.close();
		return 1;
	}

	/**
	 * 根据实习律师id获取实习律师对象
	 * @param pracLawyerId：实习律师ID
	 * @return：实习律师对象
	 */
	public TblPracticeLawyer getPracLawyerInfoFromId(int pracLawyerId){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		List result=session.createCriteria(TblPracticeLawyer.class)
		.add(Restrictions.eq("pracLawId", pracLawyerId))
		.add(Restrictions.eq("deleteLabel", 0))
		.list();
		// 提交事务
		tx.commit();
		session.close();

		//获得对应的实习律师对象
		TblPracticeLawyer tl=(TblPracticeLawyer)result.get(0);
		return tl;
	}


	/**
	 * 修改用户密码
	 * @param oldPwd：旧密码
	 * @param newPwd：新密码
	 * @param lawyerId：用户id
	 * @return：1:修改成功 0：修改失败
	 */
	public int modifyPassword(String oldPwd,String newPwd,int pracLawId){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		List result=session.createCriteria(TblPracticeLawyer.class)
		.add(Restrictions.eq("pracLawId", pracLawId))
		.list();

		TblPracticeLawyer lawyer=(TblPracticeLawyer)result.get(0);
		//得到用户密码
		String password=lawyer.getLawyerPwd();
		//判断输入的旧密码是否正确
		if(password.equalsIgnoreCase(oldPwd))
		{
			//修改用户密码
			String hql="update TblPracticeLawyer m set m.lawyerPwd=?where m.pracLawId=?";
			session.createQuery(hql)
			.setString(0, newPwd)
			.setInteger(1, pracLawId)
			.executeUpdate();
			// 提交事务
			tx.commit();
			session.close();
			return 1;
		}
		else{
			// 提交事务
			tx.commit();
			session.close();
			return 0;
		}
	}

	/**
	 * 修改用户的联系方式
	 * @param contactWay：新的联系方式
	 * @param pracLawyerId：用户id
	 * @return：1 修改成功
	 */
	public int modifyContactWay(String contactWay,int pracLawyerId){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		//修改用户密码
		String hql="update TblPracticeLawyer m set m.pracLawPhone=?where m.pracLawId=?";
		session.createQuery(hql)
		.setString(0, contactWay)
		.setInteger(1, pracLawyerId)
		.executeUpdate();
		// 提交事务
		tx.commit();
		session.close();
		return 1;
	}

	  /**
     * 新增加一个实习律师信息
     * @param tpl：实习律师对象
     * @return：1：添加成功
     */
    public int saveLawyerInfo(cn.jsj.pojo.TblPracticeLawyer tpl){
    	org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		session.save(tpl);
		// 提交事务
		tx.commit();
		session.close();
    	return 1;
    }
}